package gui.listeners;

import gui.components.Viewport;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

public class ViewportMouseListener implements MouseListener, MouseMotionListener, MouseWheelListener{
	private Viewport viewport;
	private Point mousePressPosition;
	private boolean enabled = false;
	
	public void setEnabled(boolean enabled){
		this.enabled = enabled;
	}
	
	public ViewportMouseListener(Viewport viewport){
		this.viewport = viewport;
	}

	@Override
  public void mouseWheelMoved(MouseWheelEvent e) {
		if(enabled){
			int clicks = e.getWheelRotation();
			if(clicks > 0){
				viewport.resizeImageByFactor(1 - (float)clicks / 10, e.getPoint());
			}
			else{
				viewport.resizeImageByFactor(1 + (float)clicks / 10 * -1, e.getPoint());
			}
		}
	}

	@Override
  public void mouseDragged(MouseEvent e) {
		if(enabled)
			viewport.translateByOffset(
					e.getPoint().x - mousePressPosition.x, 
					e.getPoint().y - mousePressPosition.y, true);
	}

	@Override
  public void mouseMoved(MouseEvent e) {}

	@Override
  public void mouseClicked(MouseEvent e) {}

	@Override
  public void mousePressed(MouseEvent e) {
		if(enabled){
			mousePressPosition = e.getPoint();
			viewport.storePosition();
		}
	}

	@Override
  public void mouseReleased(MouseEvent e) {}

	@Override
  public void mouseEntered(MouseEvent e) {}

	@Override
  public void mouseExited(MouseEvent e) {}
	
}
